package com.example.shop

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import com.alibaba.android.arouter.facade.annotation.Autowired
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.example.base.mvvm.BaseActivity
import com.example.shop.databinding.ActivityMainBinding
import com.example.shop.db.AppDbUtils

@Route(path = "/app/MainActivity")
class MainActivity : BaseActivity<ActivityMainBinding,LoginViewModel>() {
    /**
     * 接受ARouter跳转携带的参数
     */
   /* @JvmField
    @Autowired
    var username = ""*/
    lateinit var launcher: ActivityResultLauncher<Intent>//定义跳转后的返回值
    override fun initView() {
        /**
         * 接受ARouter跳转携带的参数
         */
       /* ARouter.getInstance().inject(this)
        Log.d("sss", "initView: ${username}")*/

        //初始化launcher --类比 拿到onActivityResult中的数据
        launcher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()){
            if(it.resultCode==102){
                val data = it.data
                if(data!=null){
                    val username = data.getStringExtra("username")//跳转页面传的参数
                    val password = data.getStringExtra("password")//跳转页面传的参数
                    binding.name = username//使用双向绑定的方式给Edittext中的etusername赋值
                    binding.pwd = password
                }

            }
        }
        binding.btnLogin.setOnClickListener {
            //登录进行前台拦截，判断用户名和密码格式
            if(binding.etUsername.text.toString().isNotEmpty()&&binding.etPassword.text.toString().isNotEmpty()){
                if(RegexUtils.isMobileSimple(binding.etUsername.text.toString())){
                    viewModel.login(binding.etUsername.text.toString(),binding.etPassword.text.toString())
                }else{
                    ToastUtils.showLong("用户名应该为手机号")
                }

            }else{
                ToastUtils.showLong("用户名和密码不能为空")
            }

        }
        //点击去注册
        binding.goRegister.setOnClickListener {
            val intent = Intent(this,RegisterActivity::class.java)
            //使用协程的方式,跳转到注册界面
            launcher.launch(intent)
        }
    }

    override fun initData() {
        viewModel.loginSuccess.observe(this){
            ToastUtils.showLong(it.message)
            if(it.code==200){
                //执行后续操作
                //缓存token字段---将用户保存--跳转到主页面
                SPUtils.getInstance().put("token",it.data.token)
                SPUtils.getInstance().put("username",it.data.username)
                AppDbUtils.getAppDb().getUserDao().insertUser(it.data)
                ARouter.getInstance().build("/app/ShopActivity").navigation()
            }else{
                //这里是失败的情况
            }
        }
        viewModel.loginFail.observe(this){
            ToastUtils.showLong("登录请求失败，原因；$it")
        }
    }

    override fun getViewModel(): Class<LoginViewModel>  = LoginViewModel::class.java

    override fun getLayout(): Int  = R.layout.activity_main

}